import logging
import os
from config.config import config_params

__log_level=config_params['log_level']
__log_file=config_params['log_file']
__log_dir = os.path.dirname(__log_file)
if not os.path.exists(__log_dir):
    os.makedirs(__log_dir)
print('now log_file & log_level params init success!')


# 配置日志
def setup_logger(name='app'):
    
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    file_handler = logging.FileHandler(__log_file, encoding='utf-8')
    file_handler.setFormatter(formatter)

    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)

    logger = logging.getLogger(name)
    logger.setLevel(__log_level)
    logger.addHandler(file_handler)
    logger.addHandler(stream_handler)

    logger.info('init logger {} success!'.format(name))
    return logger


